/*
    Android UIML

	Copyright (C) 2010  Bram Goffings (bramgoffings@gmail.com)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public License
	as published by the Free Software Foundation; either version 2.1
	of	the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

package uiml.android.executing;

import org.w3c.dom.Element;

import uiml.android.elements.Logic;
import uiml.android.elements.Part;
import uiml.android.elements.UimlElementWithAttributes;
import uiml.android.elements.peers.Vocabulary;

public class Condition extends UimlElementWithAttributes{

	// TODO comment
	private Equal equal = null;
	private Event event = null;
	private Op op = null;
	
	private Action linkedAction = null;
	
	// TODO comment
	public static String IAM = "interface";
	public static String EQUAL = "equal";
	public static String EVENT = "event";
	public static String OP = "op";
	
	/**
	 * 
	 * @param node
	 */
	public Condition(Element node){
		super(node);
		process();
	}


	/**
	 * 
	 */
	@Override
	public void processElement(Element elem) {
		if(elem.getTagName().equals(EQUAL))
			setEqual(new Equal(elem));
		else if(elem.getTagName().equals(EVENT))
			setEvent(new Event(elem));		
		else if(elem.getTagName().equals(OP))
			setOp(new Op(elem));
	}

	/**
	 * @param equal the equal to set
	 */
	public void setEqual(Equal equal) {
		this.equal = equal;
	}

	/**
	 * @return the equal
	 */
	public Equal getEqual() {
		return equal;
	}

	/**
	 * @param event the event to set
	 */
	public void setEvent(Event event) {
		this.event = event;
	}

	/**
	 * @return the event
	 */
	public Event getEvent() {
		return event;
	}

	/**
	 * @param op the op to set
	 */
	public void setOp(Op op) {
		this.op = op;
	}

	/**
	 * @return the op
	 */
	public Op getOp() {
		return op;
	}


	public String getPartNameInvoker() {
		if(event == null)
			return null;
		else
			return event.getPartName();
	}


	public void execute(Object obj, Part uiPart, Vocabulary voc, Logic logic) {
		
		// We have to pass the necessary data to our action before we add an event.
		// This is needed because we can't add params dynamicly through event handlers.
		
		if(event != null && linkedAction != null) {
			linkedAction.setInvoker(obj);
			linkedAction.setSubTree(uiPart);
			linkedAction.setVoc(voc);
			linkedAction.setLogic(logic);
			linkedAction.addEventHandler(event.getClas());
		}
	}


	public void attach(Action action) {
		linkedAction = action;
	}

}
